﻿using AutoMapper;
using B.ERP.Services;
using B.ERP.Services.Input;
using B.ERP.Services.OutPut;
using Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Standard;
using Yitter.IdGenerator;

namespace B.ERP.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ERPBomController : ControllerBase
    {
        private readonly IBOMServices _bomServices;
        public ERPBomController(IBOMServices bomServices)
        {
            _bomServices = bomServices;
        }
        private static int serialNumber = 1; // 静态变量，用于记录流水号，初始为1
        /// <summary>
        /// 编码（流水号）
        /// </summary>
        /// <param name="codeType"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public string GetCode()
        {
            var result = new ApiResult<string>();
            //获取表中的数量，在该数量上加1
            var count = _bomServices.GetBom().ResultData.Count;
            var serialNumber = count + 1;
            string serialPart = serialNumber.ToString("D7");
            string code = "JNZG-BOM" + DateTime.Now.ToString("yyyyMMddHH") + serialPart;
            serialNumber++;
            return code;

        }
        [HttpPost]
        public ApiResult<int> AddBom(AddBomInput Input)
        {
            Input.BomCode = GetCode();
           return _bomServices.AddBom(Input);
        }
        [HttpGet]
        public ApiResult<List<GetBomDTO>> GetBom()
        {
           return _bomServices.GetBom();

        }
        /// <summary>
        /// 树形
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
       
        public ApiResult<List<TreeDTO>> TreeBom()
        {
            return _bomServices.TreeBom();
        }
        
    }
}
